import * as FormSy from 'formsy-react';


const isExisty = value => value !== null && value !== undefined
const isEmpty = value => value === '';
const makeRegexp = reg => {
  if (reg instanceof RegExp) {
    return reg;
  } else if (/\/(.+)\//.test(reg)) {
    return new RegExp(RegExp.$1, 'i');
  }

  return /^$/;
};

FormSy.addValidationRule('matchRegexp', (values, value, regexp) => {
  return !isExisty(value) || isEmpty(value) || makeRegexp(regexp).test(value);
});

FormSy.addValidationRule('isUrlPath', (values, value) => {
  return !isExisty(value) || isEmpty(value) || /^[a-z0-9_\\-]+$/i.test(value);
});

FormSy.addValidationRule('maximum', (values, value, maximum) => {
  return !isExisty(value) || isEmpty(value) || (parseFloat(value) || 0) <= (parseFloat(maximum) || 0);
});

FormSy.addValidationRule('minimum', (values, value, minimum) => {
  return !isExisty(value) || isEmpty(value) || (parseFloat(value) || 0) >= (parseFloat(minimum) || 0);
});

// FormSy.addValidationRule('integer', (values, value) => {
//     return !isExisty(value) || isEmpty(value) || /^[0-9]{1, }/.test(string(value));
// });

FormSy.addValidationRule('isPhone', (values, value) => {
  return !isExisty(value) || isEmpty(value) || /^1[3456789][0-9]{9}$/.test(value);
});

FormSy.addValidationRule('isTel', (values, value) => {
  return !isExisty(value) || isEmpty(value) || /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(value);
});

FormSy.addValidationRule('isID', (values, value) => {
  return !isExisty(value) || isEmpty(value) || /(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(value);
});

FormSy.addValidationRule('isJson', (values, value) => {
  if (isExisty(value) && !isEmpty(value)) {
    try {
      JSON.parse(value);
    } catch (e) {
      return false;
    }
  }

  return true;
});

FormSy.addValidationRule('isTelOrPhone', (values, value) => {
    return !isExisty(value) || isEmpty(value) || /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(value)
        || /^1[3456789][0-9]{9}$/.test(value);
});

FormSy.addValidationRule('isRequired', (values, value) => {
    return isExisty(value) && !isEmpty(value);
});

FormSy.addValidationRule('isPro', (values, value) => {
    // 创建项目时候，是否根据type判断必填。
    // console.info(values);
    if(!values || (values.type !== "1" && values.type !== "2")) return true;
    return isExisty(value) && !isEmpty(value);
});

FormSy.addValidationRule('passwordStrict', (values, value) => {
    //
    if(!isExisty(value) || isEmpty(value)) return true;
    if (value.length < 6) {
      return false;
    }
    let Modes = 0;
    // 统计密码中字符类型
    for (let i = 0; i < value.length; i++) {
        Modes |= CharMode(value.charCodeAt(i));
    }
    console.log(Modes);
    // 判断字符类型
    function CharMode(iN) {
        if (iN >= 48 && iN <= 57) //数字
            return 1;
        if (iN >= 65 && iN <= 90) //大写字母
            return 2;
        if (iN >= 97 && iN <= 122) //小写
            return 4;
        else
            return 8; //特殊字符
    }
    // 返回密码中字符类型
    function bitTotal(num) {

        let m = 0;
        for (i = 0; i < 4; i++) {
            if (num & 1) m++;
            num >>>= 1;
        }
        return m;
    }
    // 必须是包含数字、大写小写字母和特殊字符
    return bitTotal(Modes) === 4;

});

FormSy.addValidationRule('isIDStrict', (values, value) => {
  // console.info('tset');
  if (!isExisty(value) || isEmpty(value)) return true;
  let city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
  let pass= true;
  if(!value || !/(^\d{15}$)|(^\d{17}([0-9]|X)$)/i.test(value)){
    return false;
  }
  if(!city[value.substr(0,2)]){
    return false;
  } else{
    //18位身份证需要验证最后一位校验位
    if(value.length === 18){
      value = value.split('');
      //∑(ai×Wi)(mod 11)
      //加权因子
      let factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
      //校验位
      let parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
      let sum = 0, ai = 0, wi = 0;
      for (let i = 0; i < 17; i++)
      {
          ai = value[i];
          wi = factor[i];
          sum += ai * wi;
      }
      const last = parity[sum % 11];
      if(last != value[17]){ // 这里不能是 !==，原因在于value是字符串
          pass =false;
      }
    } else {
      // 15位默认为false
      pass = false;
        // re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
        // var arrSplit = value.match(re);
        //
        // //检查生日日期是否正确
        // var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);
        // var bGoodDay;
        // bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));

    }
  }
  return pass;
});

export default {
  isEmail: 'Email 格式不正确',
  isPhone: '请输入正确的手机号码',
  isTel: '请输入正确的电话号码',
  isID: '请输入正确的身份证号码',
  isDefaultRequiredValue: '这是必填项',
  isUrl: 'Url 格式不正确',
  isInt: '请输入整数',
  isAlpha: '请输入字母',
  isNumeric: '请输入数字',
  isAlphanumeric: '请输入字母或者数字',
  isFloat: '请输入浮点型数值',
  isWords: '请输入字母',
  isUrlPath: '只能输入字母、数字、`-` 和 `_`.',
  matchRegexp: '格式不正确, 请输入符合规则为 `$1` 的内容。',
  minLength: '请输入更多的内容，至少输入 $1 个字符。',
  maxLength: '请控制内容长度, 当前文字数量超出范围，请不要超过 $1 个字符',
  maximum: '输入值超出最大范围 $1，请检查',
  minimum: '输入值超出最小范围 $1，请检查',
  isJson: '请检查 Json 格式。',
  isTelOrPhone: '请输入正确的手机号或座机',
  isIDStrict: '请输入正确的身份证号码',
  isRequired: '这是必填项',
  isPro: '这是必填项',
  passwordStrict: '密码至少六位，且必须包含数字、大写小写字母和特殊字符'
};
